﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.ApDungChuongTrinhDaoTaoService;
using eTMS.Controllers.ChuongTrinhDaoTaoService;
using eTMS.Controllers.KhoaHocService;
namespace eTMS.Controllers
{
    public class KhoaHocController : KhoaHocControllerBase
    {
        public KhoaHocController()
            : base()
        {
        }
        public static KhoaHocEntity FindKhoaHoc(Int32 KhoaHoc_ID)
        {
            KhoaHocEntity oResult = ListKhoaHocStatic.Find(delegate(KhoaHocEntity oFind)
            {
                bool result = true;
                result &= oFind.KhoaHoc_ID.Equals(KhoaHoc_ID);
                return result;
            });
            return oResult;
        }
        public void SaveAllApDungChuongTrinhActive()
        {
            ChuongTrinhDaoTaoService.ChuongTrinhDaoTaoServiceClient chuongTrinhDaoTaoServiceClient = new ChuongTrinhDaoTaoServiceClient();
            List<ChuongTrinhDaoTaoService.ChuongTrinhDaoTaoEntity> ls = new List<ChuongTrinhDaoTaoEntity>(chuongTrinhDaoTaoServiceClient.SelectAllFilter("MacDinh=1"));
            List<int> lsNganhDaoTao = new List<int>();
            ApDungChuongTrinhDaoTaoService.ApDungChuongTrinhDaoTaoServiceClient apDungChuongTrinhDaoTaoServiceClient = new ApDungChuongTrinhDaoTaoServiceClient();
            foreach (ChuongTrinhDaoTaoEntity chuongTrinhDaoTaoEntity in ls)
            {
                if (!lsNganhDaoTao.Contains(chuongTrinhDaoTaoEntity.NganhHoc_ID))
                {
                    lsNganhDaoTao.Add(chuongTrinhDaoTaoEntity.NganhHoc_ID);
                    ApDungChuongTrinhDaoTaoService.ApDungChuongTrinhDaoTaoEntity apDungChuongTrinhDaoTaoEntity = new ApDungChuongTrinhDaoTaoEntity();
                    apDungChuongTrinhDaoTaoEntity.ChuongTrinhDaoTao_ID = chuongTrinhDaoTaoEntity.ChuongTrinhDaoTao_ID;
                    apDungChuongTrinhDaoTaoEntity.KhoaHoc_ID = KhoaHocObject.KhoaHoc_ID;
                    apDungChuongTrinhDaoTaoServiceClient.Save(apDungChuongTrinhDaoTaoEntity);
                }
            }
        }
        private DataTable _TableApDung;
        public DataTable TableApDung
        {
            get
            {
                return _TableApDung;
            }
        }
        public void LoadAllDSNganhSuDungChuongTrinhDaoTao()
        {

            ApDungChuongTrinhDaoTaoServiceClient apdungSW = new ApDungChuongTrinhDaoTaoServiceClient();
            NganhHocController ctr = new NganhHocController();
            DataTable tblApDung = apdungSW.SelectAllDSWithNganhHocByKhoaHoc(KhoaHocObject.KhoaHoc_ID).Tables[0];
            _TableApDung = ctr.Table.Copy();
            _TableApDung.Columns.Add("ApDungChuongTrinhDaoTao_ID", typeof(int));
            _TableApDung.Columns.Add("ChuongTrinhDaoTao_ID", typeof(int));
            foreach (DataRow r in _TableApDung.Rows)
            {
                DataRow[] rs = tblApDung.Select("NganhHoc_ID=" + r["NganhHoc_ID"].ToString());
                if (rs.Length > 0)
                {
                    r["ApDungChuongTrinhDaoTao_ID"] = rs[0]["ApDungChuongTrinhDaoTao_ID"];
                    r["ChuongTrinhDaoTao_ID"] = rs[0]["ChuongTrinhDaoTao_ID"];
                }
                else
                {
                    r["ApDungChuongTrinhDaoTao_ID"] = 0;
                    r["ChuongTrinhDaoTao_ID"] = 0;
                }
            }
            _TableApDung.AcceptChanges();
        }
        public void SaveDSNganhSuDungChuongTrinhDaoTao()
        {
            ApDungChuongTrinhDaoTaoServiceClient apdungSW = new ApDungChuongTrinhDaoTaoServiceClient();
            foreach (DataRow r in _TableApDung.Rows)
            {
                if (r.RowState == DataRowState.Modified)
                {
                    ApDungChuongTrinhDaoTaoEntity o = new ApDungChuongTrinhDaoTaoEntity();
                    o.KhoaHoc_ID = KhoaHocObject.KhoaHoc_ID;
                    o.ChuongTrinhDaoTao_ID = ConvertInt(r["ChuongTrinhDaoTao_ID"]);
                    o.ApDungChuongTrinhDaoTao_ID = ConvertInt(r["ApDungChuongTrinhDaoTao_ID"]);
                    if (o.ApDungChuongTrinhDaoTao_ID > 0)
                    {
                        if (o.ChuongTrinhDaoTao_ID == 0)
                        {
                            apdungSW.DeleteByID(o.ApDungChuongTrinhDaoTao_ID);
                        }
                        else
                        {
                            apdungSW.Update(o);
                        }
                    }
                    else
                    {
                        if (o.ChuongTrinhDaoTao_ID > 0)
                        {
                            apdungSW.Save(o);
                        }
                    }
                }
            }
        }
    }
}
